package com.jinxin.platform.cdsideparkmgr.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.api.annotation.APCOSApi;
import com.jinxin.platform.base.api.annotation.APCOSApiOperation;
import com.jinxin.platform.base.api.constant.APCOSApiConstans;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.cdsideparkmgr.bean.CdparkmanagerProject;
import com.jinxin.platform.cdsideparkmgr.bean.CdyardmgrChargeConfig;
import com.jinxin.platform.cdsideparkmgr.bean.CdyardmgrEmplayee;
import com.jinxin.platform.cdsideparkmgr.bean.bo.OptionItem;
import com.jinxin.platform.cdsideparkmgr.common.Constant;
import com.jinxin.platform.cdsideparkmgr.common.SysOrgInfo;
import com.jinxin.platform.cdsideparkmgr.rpc.CdsideparkmgrRpc;
import com.jinxin.platform.cdsideparkmgr.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * All rights Reserved, Designed By www.beonelot.com
 *
 * @version: V1.0
 * @Copyright: www.beonelot.com Inc. All rights reserved.
 * @Description：
 * @Date: 2021-09-06
 * @Author: Lingnan
 **/
@APCOSApi(value = "车场相关接口", developers = {"lingnan"}, description = "车场相关接口")
@RestController
@RequestMapping("/cdsideparkmgr/cdsideparkmgrMyProject")
@Slf4j
public class CdsideparkmgrMyProjectController {
    @Autowired
    private ICdsideparkmgrProjectService iCdsideparkmgrProjectService;
    @Autowired
    private ICdsideparkmgrEmployeeService cdsideparkmgrEmployeeService;
    @Autowired
    private CdsideparkmgrRpc cdsideparkmgrRpc;
    @Autowired
    private ICdsideparkmgrFeeruleService cdyardmgrFeeruleService;
    @Autowired
    private ICdsideparkmgrChargeConfigService cdsideparkmgrChargeConfigService;

    @APCOSApiOperation(value = "分页查询车场", developers = {"lingnan"}, description = "分页查询车场",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/list")
    public Map<String,Object> list(@RequestParam int page, @RequestParam int limit,
                                   @RequestParam(required = false,name = "projectName")String projectName,
                                   @RequestParam(required = false,name = "projectNo")String projectNo,
                                   @RequestParam(name = "projectType")Integer projectType){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        SysOrgInfo currentOrg = cdsideparkmgrRpc.getCurrentOrg();
        if(currentOrg == null){
            resultMap.put("msg","获取当前组织信息失败");
            resultMap.put("data", null);
            return resultMap;
        }
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        //param.put("projectStatus", Constant.PROJECT_STATUS_ENABLED);
        param.put("projectType", projectType);
        param.put("projectName", projectName);
        param.put("projectNo", projectNo);
        param.put("organId", currentOrg.getOrgId());
        resultMap.put("count", iCdsideparkmgrProjectService.listPageCount(param));
        resultMap.put("data", iCdsideparkmgrProjectService.listPage2(param));
        return resultMap;
    }

    @APCOSApiOperation(value = "保存或更新车场信息", developers = {"lingnan"}, description = "保存或更新车场信息",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody CdparkmanagerProject record){
        return iCdsideparkmgrProjectService.saveOrUpdate(record);
    }

    @APCOSApiOperation(value = "删除车场", developers = {"lingnan"}, description = "删除车场",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/delete")
    public JsonResult delete(String id){
        return iCdsideparkmgrProjectService.delete(id);
    }

    @APCOSApiOperation(value = "查询所有车场", developers = {"lingnan"}, description = "查询所有车场",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/listAll")
    public JsonResult listAll(){
        return JsonResult.ok(iCdsideparkmgrProjectService.list(new HashMap()));
    }

    @APCOSApiOperation(value = "根据ID查询车场信息", developers = {"lingnan"}, description = "根据ID查询车场信息",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/detail")
    public JsonResult detail(String id){
        return iCdsideparkmgrProjectService.getByid(id);
    }

    // 车场员工相关接口
    @APCOSApiOperation(value = "车场员工相关接口", developers = {"lingnan"}, description = "车场员工相关接口",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/listEmployee")
    public Map<String,Object> listEmployee(@RequestParam(name = "yardId")String yardId){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        if(StringUtils.isEmpty(yardId)){
            resultMap.put("msg","车场id为空");
            resultMap.put("data", null);
            return resultMap;
        }
        HashMap param = new HashMap();
        param.put("yardId", yardId);
        List<CdyardmgrEmplayee> list = cdsideparkmgrEmployeeService.list(param);
        resultMap.put("count", list.size());
        resultMap.put("data", list);
        return resultMap;
    }

    @APCOSApiOperation(value = "获取角色列表", developers = {"lingnan"}, description = "获取角色列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/loadRoleList")
    public JsonResult loadRoleList(){
        JsonResult jsonResult = cdsideparkmgrRpc.loadRoleList();
        if(jsonResult.getSuccess()) {
            JSONObject jsonObj = JSONObject.parseObject(jsonResult.getData().toString());
            List<OptionItem> roleList =
                    JSONArray.parseArray(jsonObj.get("data").toString(), OptionItem.class);
            return JsonResult.ok("success",roleList);
        }
        return JsonResult.error("获取角色列表失败");
    }

    @APCOSApiOperation(value = "获取人员列表", developers = {"lingnan"}, description = "获取人员列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/loadUserList")
    public JsonResult loadUserList(){
        return cdsideparkmgrRpc.loadUserList();
    }

    @APCOSApiOperation(value = "通过ID删除员工", developers = {"lingnan"}, description = "通过ID删除员工",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/deleteEmployee")
    public JsonResult deleteEmployee(String id){
        return cdsideparkmgrEmployeeService.delete(id);
    }

    @APCOSApiOperation(value = "保存或更新员工", developers = {"lingnan"}, description = "保存或更新员工",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveEmployee")
    public JsonResult saveEmployee(@RequestBody CdyardmgrEmplayee record){
        return cdsideparkmgrEmployeeService.saveOrUpdate(record);
    }

    //收费规则相关接口
    @APCOSApiOperation(value = "收费规则相关接口", developers = {"lingnan"}, description = "收费规则相关接口",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/loadChargeRuleList")
    public JsonResult loadChargeRuleList(){
        SysOrgInfo currentOrg = cdsideparkmgrRpc.getCurrentOrg();
        if(currentOrg == null){
            return null;
        }
        return JsonResult.ok(cdyardmgrFeeruleService.listOptions(currentOrg.getOrgId()));
    }

    @APCOSApiOperation(value = "获取规则", developers = {"lingnan"}, description = "获取规则",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/getRuleByYardId")
    public JsonResult getRuleByYardId(String yardId){
        return JsonResult.ok(getByYardId(yardId));
    }

    @APCOSApiOperation(value = "保存收费规则", developers = {"lingnan"}, description = "保存收费规则",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @PostMapping("/saveCharge")
    public JsonResult saveCharge(@RequestBody CdyardmgrChargeConfig record){
        return cdsideparkmgrChargeConfigService.saveOrUpdate(record);
    }

    private CdyardmgrChargeConfig getByYardId(String yardId){
        if(StringUtils.isEmpty(yardId)){return null;}
        HashMap param = new HashMap();
        param.put("yardId",yardId);
        return cdsideparkmgrChargeConfigService.selectOne(param);
    }

    @APCOSApiOperation(value = "分页地锁车位列表", developers = {"lingnan"}, description = "分页地锁车位列表",
            infType = APCOSApiConstans.INF_TYPE_OTHER, infCategory = APCOSApiConstans.INF_CATEGORY_ALL)
    @GetMapping("/listLock")
    public Map<String,Object> listLock(@RequestParam int page, @RequestParam int limit,
                                   @RequestParam(name = "projectId")String projectId,
                                       @RequestParam(required = false,name = "lockName")String lockName){
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("code",0);
        resultMap.put("msg","success");
        HashMap param = new HashMap();
        param.put("page", page);
        param.put("limit", limit);
        param.put("lockName", lockName);
        param.put("projectId", projectId);
        resultMap.put("count", iCdsideparkmgrProjectService.listLockPageCount(param));
        resultMap.put("data", iCdsideparkmgrProjectService.listLockPage(param));
        return resultMap;
    }

}
